package com.shanghaionstar.discover.geofence;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.GeofenceInfoEntity;
import com.onstar.cn.mag.poi.Coordinate;
import com.onstar.cn.map.OMapView;
import com.onstar.cn.map.common.LocationType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.geofence.iview.IGeofencePreView;
import com.shanghaionstar.discover.geofence.presenter.GeofencePreviewPresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.MapHandler;

/**
 * Created by 7828wc on 16/7/21.
 */
@ContentView(R.layout.discover_geofence_preview)
public class GeofencePreviewActivity extends AbstractMapActivity implements IGeofencePreView {
//    @FindViewById
//    private TopLayout top_title;
    @FindViewById
    private OMapView map_view;
    @FindViewById
    private LinearLayout bottom_detail_view;
    @FindViewById
    private TextView tv_geofence_name, tv_geofence_center, tv_geofence_mobile, tv_geofence_range;

    private GeofencePreviewPresenter presenter;
    private MapHandler mapHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        map_view.onCreate(savedInstanceState);
        presenter = new GeofencePreviewPresenter(getScheduler());
        presenter.setView(this);
        presenter.getIntent();
    }

    @Override
    public BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    public void displayView(GeofenceInfoEntity detailGeoFenceInfo) {
        if (detailGeoFenceInfo != null) {
            mapHandler = new MapHandler(GeofencePreviewActivity.this, map_view.getMap());
            Coordinate coordinate = detailGeoFenceInfo.getCenterPoiCoordinate();
            if(coordinate.getLongitude()>=-180&&coordinate.getLongitude()<=180&&coordinate.getLatitude()>=-90&&coordinate.getLatitude()<=90) {
                mapHandler.setGeoFenceCircle(detailGeoFenceInfo);
            }
            tv_geofence_name.setText(detailGeoFenceInfo.getGeoFencingName());
            tv_geofence_center.setText(detailGeoFenceInfo.getCenterPoiAddress());
            tv_geofence_mobile.setText(detailGeoFenceInfo.getMobilePhone());
            tv_geofence_range.setText(detailGeoFenceInfo.getRange() + presenter.getRangeUnit(detailGeoFenceInfo));

            bottom_detail_view.setVisibility(View.VISIBLE);
        } else {
            bottom_detail_view.setVisibility(View.GONE);
        }
    }

    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(Object object) {

    }
    @Override
    public Activity onActivity() {
        return this;
    }

    @Override
    public void initMapHandler(MapHandler mapHandler) {
        mapHandler = new MapHandler(GeofencePreviewActivity.this, map_view.getMap());
    }

    @Override
    public void showPoiToMap(PoiItemInfo poiItemInfo, LocationType locationType) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.finish();
        presenter = null;
    }
}
